<?php

error_reporting(-1);
ini_set('display_errors', 1);

require APPPATH . 'core/feed/feed_bank.php';

class home extends CI_Controller {

    //array for view
    protected $data;

    function __construct() {
        $this->_initdata();
    }

    function index() {
        //get tin-noi-bat (top page)
        list($this->data['aFeatureNews']) = get_features_news($this->Frontendmodel);

        //get News list (cateid=2)
        $news_cate = "van-ban-phap-quy";
        list($this->data['aNews2']) = getContentIndex("docs_id", "docs", $this->Frontendmodel);
        $this->data['news_cate_2'] = $news_cate;

        //get News list (cateid=3)
        $news_cate = "kien-thuc-xnk";
        $news_cate_id = NEWS_CATE_KTXNK;
        list($this->data['aNews3']) = getContentIndex("id", "knowledge", $this->Frontendmodel);
        $this->data['news_cate_3'] = $news_cate;

        //get News list (cateid=4)
        $news_cate = "thu-tuc-hai-quan";
        $news_cate_id = NEWS_CATE_TTHQ;
        list($this->data['aNews4']) = getContentIndex("customs_id", "customs_procedures", $this->Frontendmodel);
        $this->data['news_cate_4'] = $news_cate;

        $news_cate = "nghiep-vu-giao-nhan";
        list($this->data['aNews5']) = getContentIndex("id", "forwarding", $this->Frontendmodel);
        $this->data['news_cate_5'] = $news_cate;

        //get FAQ list (box Hoi/Dap)
        list($this->data['aFAQ']) = get_home_faq($this->Frontendmodel);
        
        //get box video
        list($count, $this->data['aVideo']) = _get_list_video($this->Frontendmodel, 0, 4, "");

        //get Adv list (box Rao Vặt)
        list($this->data['aADV'])  = getContentIndex("adv_id", "adv2013", $this->Frontendmodel);
        
        $this->data['aCityList'] = get_cities_list($this->Frontendmodel);
        
        //get content of VBPQ
        list($count_docs, $this->data['aDocs']) = get_docs_desc_paging($this->Fe_docs_model, "", 40, 0, 4);
        
        //get content of TTHQ
        list($count_customs, $this->data['aCustoms']) = $this->Frontendmodel->_get_list_customs("", "", 4, 1);
        
        $this->show($this->data);
    }

    //controller: Thông tin doanh nghiệp
    function company() {
        $page_paging = $this->config->item('base_url') . "thong-tin-doanh-nghiep/page";
        $this->show_company_list($page_paging);
    }

    //Thông tin doanh nghiệp
    function show_company_list($page_paging) {
        $iCurPage = 1;
        $iCurPage = $this->uri->segment(3);
        $iRow_Count = 0;
        $limit = PAGESIZE_COMPANY;
        $start = $iCurPage * PAGESIZE_COMPANY - PAGESIZE_COMPANY;
        if (!($start > 0)) {
            $start = 0;
        }
        $eu = ($start - 0);
        if (!$limit > 0) {
            $limit = 20;
        }
        list($iRow_Count, $this->data['aCompany_Adv']) = get_company_paging($this->Fe_company_model,$eu, $limit);
        $this->data['paging'] = $this->paging($iCurPage, ceil($iRow_Count / PAGESIZE_COMPANY), $page_paging, $iRow_Count);
        $this->data['cur_page'] = $iCurPage;
        $this->data['previous_id'] = 1;
        $this->load->view('ajax/thongtindoanhnghiep.php', $this->data);
    }

    //Phân trang news
    function paging($page, $totalpage, $pagelink, $totalrows) {
        if ($page == "")
            $page = 1;

        $this->data['cur_page'] = $page;

        $paging_str = '';
        if ($page > 1)
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next" onclick="get_company_adv('
                    . ($page - 1) . ')"><img src="' . $this->config->item('img_url')
                    . 'style/images/img20.gif" border="0"></a>';
        else
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next"><img src="'
                    . $this->config->item('img_url') . 'style/images/img20.gif" border="0"></a>';

        // list 5 pagelink
        $startpage = 1;
        if ($page > 3)
            $startpage = $page - 2;
        $endpage = $totalpage;
        if ($totalpage - $page > 3)
            $endpage = $page + 5;
        for ($i = $startpage; $i < $page; $i++)
            $paging_str .= '<a href="javascript:void(0);" class="paging-item" onclick="get_company_adv(' . $i . ')">' . $i . '</a>';

        $paging_str .= '<a href="javascript:void(0);" class="paging-item-selected">' . $page . '</a>';

        if ($page == 2)
            $a = 2;
        else if ($page < 3)
            $a = 1;
        else
            $a = 3;
        for ($i = $page + 1; $i <= $endpage - $a; $i++)
            $paging_str .= '<a href="javascript:void(0);" class="paging-item" onclick="get_company_adv(' . $i . ')">' . $i . '</a>';

        if ($page < $totalpage)
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next" onclick="get_company_adv(' . ($page + 1) . ')"><img src="' . $this->config->item('img_url') . 'style/images/img21.gif" border="0"></a>';
        else
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next"><img src="' . $this->config->item('img_url') . 'style/images/img21.gif" border="0"></a>';
        $paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';

        return $paging_str;
    }

    function _check_page_views($id) {
        if ($this->Frontendmodel->_check_views("PAGEVIEWS") == false) {
            $page_views = $this->Frontendmodel->_update_config($id);
            $this->Frontendmodel->_set_session("PAGEVIEWS", $page_views);
        }
    }

    function _load_language() {
        //language chỉnh trong file: system/language/vn/home_lang.php
        $this->lang->load('home', 'vn');
        $this->data['page_title'] = $this->lang->line('page_title');
        $this->data['page_latest_news'] = $this->lang->line('page_latest_news');
        $this->data['page_kienthucxnk'] = $this->lang->line('page_kienthucxnk');
        $this->data['page_nghiepvugiaonhan'] = $this->lang->line('page_nghiepvugiaonhan');
        $this->data['page_nghiepvukhac'] = $this->lang->line('page_nghiepvukhac');
        $this->data['page_thutuchaiquan'] = $this->lang->line('page_thutuchaiquan');
        $this->data['page_vanbanphapquy'] = $this->lang->line('page_vanbanphapquy');
        $this->data['page_tinnoibat'] = $this->lang->line('page_tinnoibat');
        $this->data['page_nodata'] = $this->lang->line('page_nodata');
        $this->data['page_faq'] = $this->lang->line('page_faq');
        $this->data['page_video'] = $this->lang->line('page_video');
        $this->data['page_more'] = $this->lang->line('page_more');
        $this->data['page_css'] = $this->lang->line('page_css');
        $this->data['page_header'] = $this->lang->line('page_header');
        $this->data['keyword'] = $this->lang->line('keyword');
        $this->data['page_name'] = $this->lang->line('page_name');
        $this->data['page_footer'] = $this->lang->line('page_footer');
        
        $this->data['page_description'] = "";
        $this->data['page_keywords'] = "";
        $this->data['page_image'] = "";
    }

    function _initdata() {
        parent::__construct();
        session_start();

        $this->load->helper('eximcached');

        $this->load->model('Frontendmodel');
        $this->load->model('Fe_adv_model');
        $this->load->model('Fe_company_model');
        $this->load->model('Fe_docs_model');
        $this->load->model('Fe_forwarding_model');
        $this->load->model('Fe_knowledge_model');
        $this->load->model('Fe_faq_model');
        
        //load submenu and footer
        list($this->data['aLatestNews']) = get_latest_news($this->Frontendmodel, 20);
        list($this->data['aCate2']) = get_newscate_list(NEWS_CATE_VBPQ, $this->Frontendmodel);
        $this->data['aCate3'] = $this->Fe_knowledge_model->get_all_cate();
        $this->data['aCate4'] = _get_all_cate_customs($this->Frontendmodel);
        $this->data['aCate5'] = $this->Fe_forwarding_model->get_all_cate();
        $this->data['aCate6'] = $this->Fe_faq_model->_get_faq_cate_list();

        //load language for home page:
        $this->_load_language();
        $this->data['base_url'] = $this->config->item('base_url');
        $this->data['img_url'] = $this->config->item('img_url');

        //load feed:
        $feed = new feed_bank();
        $this->data['gold'] = get_feed_gold($feed);
        $this->data['exchange_rate'] = get_feed_exchange($feed);
    }

    function get_weather($woeid) {
        $data = get_feed_weather($this);
        foreach ($data as $city) {
            if ($city['woeid'] == $woeid)
                echo $city['data'];
        }
    }

    function get_feed_weather_from_file() {
        $data = simplexml_load_file(realpath(APPPATH) . "/data/weather/weather.xml");
        foreach ($data->city as $c) {
            $city['woeid'] = (string) $c->woeid;
            $city['data'] = (string) $c->data;
            $arr[] = $city;
        }
        return $arr;
    }

}

?>
